﻿using Client.Constant;
using Client.Framework.Event;
using Client.Framework.Message;
using Client.Framework.Socket;
using Client.Message.Request;
using Client.Message.Response;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Client.ViewModel.MainViewModel
{
    /// <summary>
    /// Author: Leil
    /// Description: LoginViewModel
    /// CreateTime: 2020/6/1 17:33:50
    /// </summary>
    public class LoginViewModel : BindableBase
    {
        public LoginWindow LoginWindow { get; private set; }
        public LoginViewModel(LoginWindow loginWindow)
        {
            LoginWindow = loginWindow;
            LoginCommand = new DelegateCommand(Login);
            MessageEventPublisher.SubscribeEvent(MessageId.RESPONSE_LOGIN, LoginResponse);
        }


        private string username;

        public string Username
        {
            get { return username; }
            set
            {
                username = value;
                RaisePropertyChanged("Username");
            }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        private string tipStr;

        public string TipStr
        {
            get { return tipStr; }
            set
            {
                tipStr = value;
                RaisePropertyChanged("TipStr");
            }
        }

        private Visibility showTip = Visibility.Hidden;

        public Visibility ShowTip
        {
            get { return showTip; }
            set
            {
                showTip = value;
                RaisePropertyChanged("ShowTip");
            }
        }


        public DelegateCommand LoginCommand { get; set; }
        public void Login()
        {
            if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
            {
                TipStr = "用户名或密码不能为空";
                ShowTip = Visibility.Visible;
                return;
            }
            ShowTip = Visibility.Hidden;
            LoginReq msg = new LoginReq(MessageId.REQUEST_LOGIN)
            {
                Username = Username,
                Password = Password
            };
            ApplicationClient.Instance().Send(msg);
        }


        private void LoginResponse(ResponseBaseMessage message)
        {
            if (message is LoginResp msg)
            {
                if(msg.Code == 200)
                {
                    // 将当前登录用户写入内存中
                    ApplicationClient.Instance().CurrentLoginUser = msg.LoginUser;
                    // 登录成功
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        MainWindow mainWindow = new MainWindow(LoginWindow);
                        LoginWindow.Close();
                        mainWindow.Show();
                    }));
                } else if (msg.Code == 500)
                {
                    TipStr = msg.ErrMsg;
                    ShowTip = Visibility.Visible;
                }
            }
        }

    }
}
